package com.irs.controller;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.irs.annotation.SysLog;
import com.irs.pojo.TbCategory;
import com.irs.pojo.TbUsers;
import com.irs.service.CategoryService;
import com.irs.util.ResultUtil;



@Controller
@RequestMapping("category/")
public class CategoryController {
	
	@Autowired
	private CategoryService categoryServiceImpl;
	
	@RequestMapping("categoryList")
	@RequiresPermissions("category:category:list")
	public String selCategory(){
		return "page/article/categoryList";
	}
	@RequestMapping("addCategory")
	@RequiresPermissions("category:category:save")
	public String addCategory(){
		return "page/article/addCategory";
	}
	@RequestMapping("editCategory/{id}")
	@RequiresPermissions("category:category:update")
	public String editCategory(@PathVariable("id")String id,Model model){
		TbCategory category = categoryServiceImpl.selCategoryById(id);
		model.addAttribute("category",category);
		return "page/article/editCategory";
	}
	
	@RequestMapping("getCategoryList")
	@RequiresPermissions("category:category:list")
	@ResponseBody
	public ResultUtil getCategoryList(Integer page,Integer limit){
		ResultUtil categorys = categoryServiceImpl.selCategorys(page, limit);
		return categorys;
	}
	
	
	
	@SysLog(value="添加分类")
	@RequestMapping("insCategory")
	@RequiresPermissions("category:category:save")
	@ResponseBody
	public ResultUtil insCategory(TbCategory category){
		TbCategory c =  categoryServiceImpl.selCategoryByname(category.getCategory());
		if(c!=null){
			return  new ResultUtil(500,"分类已存在!");
		}else{
			categoryServiceImpl.insCategoryService(category);
			return ResultUtil.ok();
		}
	
	}
	
	/**
	 * 根据ID删除分类
	 * @param id
	 * @return
	 */
	@SysLog(value="根据ID删除分类")
	@RequestMapping("delCategoryByid")
	@RequiresPermissions("category:category:delete")
	@ResponseBody
	public ResultUtil delCategoryById(String id){
		try {
			categoryServiceImpl.delCategoryById(id);
			return ResultUtil.ok();
		} catch (Exception e) {
			e.printStackTrace();
			return ResultUtil.error();
		}
		
	}
	
	/**
	 * 根据ID批量删除分类
	 * @param id
	 * @return
	 */
	@SysLog(value="根据ID批量删除分类")
	@RequestMapping("delCategory")
	@RequiresPermissions("category:category:delete")
	@ResponseBody
	public ResultUtil delCategory(String categoryStr){
		try {
			categoryServiceImpl.delCategorysService(categoryStr);
			return ResultUtil.ok();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return ResultUtil.error();
		}
			
	}
	
	/**
	 * 根据ID修改分类
	 * @param categiry
	 * @return
	 */
	@SysLog(value="更新分类信息")
	@RequestMapping("updCategory")
	@RequiresPermissions("category:category:update")
	@ResponseBody
	public ResultUtil updateCategory(TbCategory category){
		TbCategory c =  categoryServiceImpl.selCategoryByname(category.getCategory());
		if(c!=null){
			return new ResultUtil(500,"分类已存在!");
		}else{
			categoryServiceImpl.updateCategoryById(category);
			return ResultUtil.ok();
		}
		
	}
	
	
	
}
